package com.capgemini.diabeo.service;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.util.ByteArrayBuffer;

import com.capgemini.diabeo.R;
import com.capgemini.diabeo.model.Category;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.model.DishDao;
import com.capgemini.diabeo.model.DishDao.Properties;
import com.capgemini.diabeo.model.Menu;
import com.capgemini.diabeo.model.MenuDish;
import com.capgemini.diabeo.model.MenuDishDao;
import com.googlecode.androidannotations.annotations.EBean;

import de.greenrobot.dao.Property;

@EBean
public class DishService extends AbstractDatabaseService {
	
	public void insertImages(Category category)
	{
		DishDao dishDao = dbHelper.getDishDao();
		List<Dish> lalala = retrieveDishList(category);
			for (Dish dish : lalala) {
				dish.setImage(convertImageToBytes("dishCruditesVinaigette.jpg"));
				dishDao.update(dish);
			}
	}

	public void createDishes(int nb, Category category) {
		List<Dish> dishes = new ArrayList<Dish>();
		//		for (int i = 0; i < nb; i++) {
		//			Dish dish = new Dish();
		//			dish.setName("dish" + i);
		//			dish.setCarbohydrate(2.0);
		//			dish.setIg(2.0);
		//			dish.setLipid(2.0);
		//			dish.setCategory(category);
		//			dishes.add(dish);
		//		}



		if (category.getName().equals(
				context.getString(R.string.title_catogary_entree))) {
			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_crudite));
			dish1.setCarbohydrate(5.0);
			dish1.setIg(1.0);
			dish1.setLipid(1.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishCruditesVinaigette.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_legumecuit));
			dish2.setCarbohydrate(5.0);
			dish2.setIg(1.0);
			dish2.setLipid(1.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishLegumescuitsVinaigrette.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_carotte));
			dish3.setCarbohydrate(10.0);
			dish3.setIg(1.0);
			dish3.setLipid(1.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishCarottesRapees.jpg"));
			dishes.add(dish3);
		} else if (category.getName().equals(
				context.getString(R.string.title_catogary_plats))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_platdujourlegume));
			dish1.setCarbohydrate(10.0);
			dish1.setIg(1.0);
			dish1.setLipid(3.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishPlatJourLegumes.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_platdujourfeculent));
			dish2.setCarbohydrate(40.0);
			dish2.setIg(1.0);
			dish2.setLipid(3.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishPlatJourFeculents.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_platdujour));
			dish3.setCarbohydrate(30.0);
			dish3.setIg(1.0);
			dish3.setLipid(3.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishPlatJourLegumesFeculents.jpg"));
			dishes.add(dish3);
		} else if (category.getName().equals(
				context.getString(R.string.title_catogary_acommpagne))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_legumevert));
			dish1.setCarbohydrate(5.0);
			dish1.setIg(1.0);
			dish1.setLipid(0.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishLegumesVerts.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_choucroute));
			dish2.setCarbohydrate(5.0);
			dish2.setIg(1.0);
			dish2.setLipid(0.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishChoucroute.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_citrouille));
			dish3.setCarbohydrate(8.0);
			dish3.setIg(3.0);
			dish3.setLipid(0.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishCitrouille.jpg"));
			dishes.add(dish3);
		} else if (category.getName().equals(
				context.getString(R.string.title_catogary_restauration))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_beignet));
			dish1.setCarbohydrate(20.0);
			dish1.setIg(2.0);
			dish1.setLipid(1.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishBeignetsCrevettes.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_Nem));
			dish2.setCarbohydrate(15.0);
			dish2.setIg(2.0);
			dish2.setLipid(1.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishNems.JPG"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_samossa));
			dish3.setCarbohydrate(10.0);
			dish3.setIg(2.0);
			dish3.setLipid(1.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishSamossa.JPG"));
			dishes.add(dish3);
		}  else if (category.getName().equals(
				context.getString(R.string.title_catogary_dessert))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_fromagemoy));
			dish1.setCarbohydrate(0.0);
			dish1.setIg(0.0);
			dish1.setLipid(1.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishFromageBlancPortion.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_fromageblanc));
			dish2.setCarbohydrate(5.0);
			dish2.setIg(1.0);
			dish2.setLipid(0.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishFromageBlancUnite.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_fromagefruit));
			dish3.setCarbohydrate(15.0);
			dish3.setIg(1.0);
			dish3.setLipid(0.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishFromageBlancFruits.jpg"));
			dishes.add(dish3);
		} else if (category.getName().equals(
				context.getString(R.string.title_catogary_pain))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_viennoiserie));
			dish1.setCarbohydrate(10.0);
			dish1.setIg(2.0);
			dish1.setLipid(0.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishMiniViennoiserie.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_croissant));
			dish2.setCarbohydrate(20.0);
			dish2.setIg(2.0);
			dish2.setLipid(1.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishCroissant.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_brioche));
			dish3.setCarbohydrate(30.0);
			dish3.setIg(2.0);
			dish3.setLipid(1.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishBrioche.jpg"));
			dishes.add(dish3);
		} else if (category.getName().equals(
				context.getString(R.string.title_catogary_boisson))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_the));
			dish1.setCarbohydrate(0.0);
			dish1.setIg(0.0);
			dish1.setLipid(0.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishThe.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_capuccino));
			dish2.setCarbohydrate(5.0);
			dish2.setIg(1.0);
			dish2.setLipid(0.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishCapuccino.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_chocolat));
			dish3.setCarbohydrate(20.0);
			dish3.setIg(1.0);
			dish3.setLipid(1.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishChocolatLait.jpg"));
			dishes.add(dish3);
		}else if (category.getName().equals(
				context.getString(R.string.title_catogary_aperitif))) {

			Dish dish1 = new Dish();
			dish1.setName(context.getString(R.string.title_dish_barre));
			dish1.setCarbohydrate(15.0);
			dish1.setIg(2.0);
			dish1.setLipid(0.0);
			dish1.setCategory(category);
			dish1.setImage(convertImageToBytes("dishBarreCereales.jpg"));
			dishes.add(dish1);

			Dish dish2 = new Dish();
			dish2.setName(context.getString(R.string.title_dish_barrechoco));
			dish2.setCarbohydrate(30.0);
			dish2.setIg(2.0);
			dish2.setLipid(0.0);
			dish2.setCategory(category);
			dish2.setImage(convertImageToBytes("dishBarreChocolatee.jpg"));
			dishes.add(dish2);

			Dish dish3 = new Dish();
			dish3.setName(context.getString(R.string.title_dish_biscuit));
			dish3.setCarbohydrate(10.0);
			dish3.setIg(2.0);
			dish3.setLipid(0.0);
			dish3.setCategory(category);
			dish3.setImage(convertImageToBytes("dishBiscuitAperitif.jpg"));
			dishes.add(dish3);
		}

		DishDao dishDao = dbHelper.getDishDao();
		dishDao.insertInTx(dishes);
	}

	public List<Dish> retrieveDishList(Category category) {
		DishDao dishDao = dbHelper.getDishDao();
		Property teeest = Properties.IdCategory;
		Long lalalala = category.getId();
		List<Dish> dishes = dishDao.queryBuilder()
				.where(Properties.IdCategory.eq(category.getId())).list();
		return dishes;
	}
	
	public List<Dish> getHistoriqueDishs(Menu menu){
		
		List<Dish> dishes = new ArrayList<Dish>();
		
			
			MenuDishDao menuDishDao = dbHelper.getMenuDishDao();
			List<MenuDish> dishIDrList = menuDishDao.queryBuilder().where(com.capgemini.diabeo.model.MenuDishDao.Properties.IdMenu.eq(menu.getId())).list();
		
			for (MenuDish menuDish : dishIDrList) {
				
				DishDao dishDao = dbHelper.getDishDao();
				dishes.addAll(dishDao.queryBuilder().where(Properties.Id.eq(menuDish.getIdDish())).list());
				
			}
		
		return dishes;
	}
	
	

	public byte[] convertImageToBytes(String path)
	{
		InputStream is;
		byte[] imageBytes = null;
		try {
			
			InputStream ims = this.context.getAssets().open(path);
			ByteArrayOutputStream stream=new ByteArrayOutputStream();
            BufferedInputStream bis = new BufferedInputStream(ims);

            ByteArrayBuffer baf = new ByteArrayBuffer(500);
            int current = 0;
            while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
            }

            imageBytes = baf.toByteArray();
            
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*	 Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images);
		    ByteArrayOutputStream stream=new ByteArrayOutputStream();
		    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
		    byte[] image=stream.toByteArray();
		    System.out.println("byte array:"+image);

		    String img_str = Base64.encodeToString(image, 0);
		    System.out.println("string:"+img_str);*/
		return imageBytes;
	}

}
